#include"5-19.h"
void HeaoInit(HP* php) {
	php->data = (HPDataType*)malloc(sizeof(HPDataType) * 4);
	php->size = 0;
	php->capacity = 4;
}
void Swap(HPDataType* min, HPDataType* big) {
	HPDataType tmp;
	tmp = *min;
	*min = *big;
	*big = tmp;
}
void  AdjustDwon(HPDataType* arr,int size,int parent) {
	int child = parent *2 +1;
	while (child+1<size && child<size){
		if (child+1<size&&arr[child + 1] > arr[child ]) {
			child += 1;
		}
		if (arr[child] > arr[parent]) {
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else {
			break;
		}
		
	}
}

void AdjustUp(HPDataType* arr , int child) {
	int parent = (child - 1) / 2;
	while (child<0) {
		if (arr[child] < arr[parent]) {
			Swap(&arr[child], &arr[parent]);
			child = parent;
			parent = (parent - 1) / 2;
		}
		else{
			break;
		}
	}
}
void HeaoPush(HP* php,HPDataType x) {
	assert(php);
	if (php->size == php->capacity) {
		php->data = (HPDataType*)realloc(php->data, php->capacity * 2);
		php->capacity *= 2;
	}

	php->data[php->size] = x;
	php->size++;
	AdjustUp(php->data, php->size - 1);

}
void HeapDestroy(HP* php) {
	free(php->data);
	free(php);
}
void HeapPop(HP* php) {
	assert(php);
	assert(php->size < 0);
	Swap(php->data[php->size - 1], php->data[0]);
	AdjustDwon(php,php->size,0);
}
HPDataType HeapTop(HP* php) {
	return php->data[0];
}
bool HeapEmpty(HP* php) {
	return php->size != 0;
}
int HeapSize(HP* php) {
	return php->size;
}